/**
 * Created by DELL on 2018/9/14.
 */
(function (ns) {
  var Login = ns.Login = function () {
    this.login_source = '';  // app / weixin /other
    this.init();
  };
  Login.prototype = {
    init: function () {
      this.checkLogin();
    },
    checkLogin: function () {
      var uid = this.get_uid();
      if (!uid) {
        if (tools.is_wxBrowser()) {        // 微信中直接授权
          if (window.location.hostname === 'www.newscctv.net') { // 央视的地址才授权
            this.authorization();
          } else {
            this.login_end()
          }
        } else {                           // 其他浏览器
          if ($.cookie && $.cookie('newscctv_uid')) {  // app中会注入cookie
            this.login_source = 'app';
            this.getUserInfo_app();
          } else {
            console.log('无效登陆途径');
            this.login_source = 'other';
            if (_isDevelopment) {
            } else {
              $('.page9').fadeIn();
            }
          }
          this.login_end()
        }
      } else {         // 用户已登录
        this.login_end()
      }
    },
    get_uid: function () {
      return localStorage.getItem('2018music_uid');
    },
    // 授权
    authorization: function () {
      var _this = this;
      var _params = tools.getParams();
      var code = _params['code'];
      var appID = _config.appID;
      if (!code) {
        var callbackurl = window.location.href;
        // 请求此链接若微信用户同意授权重定向到设置的URL中code也会加载到其中
        var jump_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appID + "&redirect_uri=" + encodeURIComponent(callbackurl) + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        window.location.href = jump_url;
      } else {
        // 第三方登录授权获取access_token
        $.ajax({
          type: 'get',
          url: _api.getWxUserInfo,
          dataType: 'json',
          data: {
            appid: appID,
            code: code
          }
        }).done(function (resp) {
          console.log(resp, 'result.data');
          if (resp.result / 1 === 0 ){
            var uid = resp.data.openid;
            var avatar = resp.data.headimgurl.replace('http://', 'https://');
            var nickname = resp.data.nickname;

            localStorage.setItem('2018music_uid', uid);
            localStorage.setItem('2018music_avatar', avatar);
            localStorage.setItem('2018music_nickname', nickname);
            _this.login_source = 'weixin';
            _this.login_end()

          } else {
            alert('获取用户信息失败：' + resp.msg)
          }
        }).fail(function () {
          alert('获取用户信息失败： 内部服务器错误');
        })
      }
    },
    getUserInfo_app: function () {
      var uid = $.cookie('newscctv_uid');
      var nickname = $.cookie('newscctv_user_nickname');
      var avatar = $.cookie('newscctv_user_avatar');
      var shanghe_authsign = $.cookie('newscctv_auth_sign');

      localStorage.setItem('2018music_uid', uid);
      localStorage.setItem('2018music_nickname', nickname);
      localStorage.setItem('2018music_avatar', avatar);
    },
    getUserInfo: function () {
      // 是否合法
      var is_legitimate = false;
      var uid = localStorage.getItem('2018music_uid');
      if (uid) {
        is_legitimate = true
      } else {
        uid = 'C7458BAFC95DE4495B71F43A969EEB89';  // 测试账号
        if (_isDevelopment) { // 测试环境
          is_legitimate = true
        }
      }
      var nickname = localStorage.getItem('2018music_nickname') || '未知用户';
      var avatar = localStorage.getItem('2018music_avatar') || '';
      // 登陆途径是否非法
      return {
        uid: uid,
        nickname: nickname,
        avatar: avatar,
        is_legitimate: is_legitimate,
        login_source: this.login_source
      }
    },
    // 登录完毕
    login_end: function () {
      var userInfo = this.getUserInfo();
      // alert(userInfo.uid)
      app.login_success_back(userInfo);
      $('.loading').hide();
    }

  }
})(window.app);